#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re

import xlwt

# 生成新的语言包路径
new_strings_path = '../strings_new_ios.txt'

# 读取语言文件路径文件路径
string_file = '../ios.txt'
# 生成表格文件路径
save_excel_path = '../strings_excel_ios.xls'
# 表名
sheet_name = "chinese"
pattern = r'"(?P<key>[\s\S]+)"\s*=\s*"(?P<value>(.|\n)+)";'


def split_str():
    f = open(string_file, 'r', encoding='UTF-8')
    dir = {}

    line = f.readline()
    while line:

        if len(line) <= 1:
            print(line, "--------------------")
            line = f.readline()
            continue
        nameSearch = re.search(pattern, line, 0)
        if nameSearch is not None:
            # result = re.search(pattern, line, 0)
            # print nameSearch.group()
            # print nameSearch.group(1),nameSearch.group(2)

            if nameSearch.group('key') is not None and nameSearch.group('value') is not None:
                print(nameSearch.group('key'), '------>', nameSearch.group('value'))
                dir[nameSearch.group('key')] = nameSearch.group('value')
            else:
                print("有空的值:", nameSearch.group(1), nameSearch.group(2))

        line = f.readline()
    # // pattern1 = r'name="[a-zA-Z_]*"'
    f.close()
    return dir


def set_style(name='Times New Roman', height=220, bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font
    font.bold = bold
    font.name = name
    font.colour_index = 4
    font.height = height
    style.font = font
    return style


def create_excel():
    dir = split_str()
    if len(dir) > 0:
        exc = xlwt.Workbook(encoding='utf-8')
        sheet = exc.add_sheet(sheet_name, True)
        keys = list(dir.keys())
        for index in range(0, len(keys)):
            # print index
            # print keys[index],dir.get(keys[index])
            sheet.write(index, 0, keys[index])
            sheet.write(index, 1, dir.get(keys[index]))

        exc.save(save_excel_path)


# 主函数
def main():
    # read_file()
    # split_str()
    create_excel()


if __name__ == "__main__":
    main()
